﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class NhanVienDAO
    {
        public static DataClasses1DataContext data = new DataClasses1DataContext();

        public static NhanVien IsExisting(string TenDangNhap, string MatKhau)
        {
            try
            {
                List<NhanVien> list = new List<NhanVien>();
                var query = data.NhanViens.Where(mem => (mem.Username.Trim() == TenDangNhap) && (mem.Password.Trim() == MatKhau));
                foreach (NhanVien nv in query)
                {
                    list.Add(nv);
                }
                return list[0];
            }
            catch
            {
                return null;
            }
        }

        //Select * From
        public static List<NhanVien> LayDSNV()
        {
            try
            {
                var query = from nhanvien in data.NhanViens select nhanvien;
                List<NhanVien> list = new List<NhanVien>();
                foreach (NhanVien nv in query)
                {
                    list.Add(nv);
                }
                return list;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Insert into
        public static bool ThemNV(NhanVien nv)
        {
            try
            {
                data.NhanViens.InsertOnSubmit(nv);
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Update Where
        public static bool CapNhatNV(NhanVien m_nv)
        {
            try
            {
                NhanVien nv = data.NhanViens.Single(nhanvien => nhanvien.ID == m_nv.ID);
                nv.HoTen = m_nv.HoTen;
                nv.Luong = m_nv.Luong;
                nv.DiaChi = m_nv.DiaChi;
                nv.CMND = m_nv.CMND;
                nv.DienThoai = m_nv.DienThoai;
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Delete From Where
        public static bool XoaNV(int ma)
        {
            try
            {
                NhanVien nv = data.NhanViens.Single(nhanvien => nhanvien.ID == ma);
                data.NhanViens.DeleteOnSubmit(nv);
                data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //Search
        public static List<NhanVien> TimKiemHoTen(string stringSearch)
        {
            try
            {
                var query = from nhanvien in data.NhanViens select nhanvien;
                List<NhanVien> ResultSearch = new List<NhanVien>();
                foreach (NhanVien nv in query)
                {
                    if (nv.HoTen.Contains(stringSearch) || nv.HoTen.ToLower().Contains(stringSearch))
                        ResultSearch.Add(nv);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }

        public static List<NhanVien> TimKiemDiaChi(string stringSearch)
        {
            try
            {
                var query = from nhanvien in data.NhanViens select nhanvien;
                List<NhanVien> ResultSearch = new List<NhanVien>();
                foreach (NhanVien nv in query)
                {
                    if (nv.DiaChi.Contains(stringSearch) || nv.DiaChi.ToLower().Contains(stringSearch))
                        ResultSearch.Add(nv);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }

        public static List<NhanVien> TimKiemDienThoai(string stringSearch)
        {
            try
            {
                var query = from nhanvien in data.NhanViens select nhanvien;
                List<NhanVien> ResultSearch = new List<NhanVien>();
                foreach (NhanVien nv in query)
                {
                    if (nv.DienThoai == stringSearch)
                        ResultSearch.Add(nv);
                }
                return ResultSearch;
            }
            catch
            {
                return null;
            }
        }
    }
}
